package cn.com.ghy.android.bean;

import java.io.InputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import android.util.Xml;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:用户登录信息解析器
 * @author wuz  @date 2011-5-11 10:06:35
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class LoginInfoBeanParser {
        
    public static String createXML(List<LoginInfoBean> list, Writer writer) 
            throws Exception {
        XmlSerializer xmlSerializer = Xml.newSerializer();
        
        xmlSerializer.setOutput(writer);
        xmlSerializer.startDocument("UTF-8", true);
        xmlSerializer.endDocument();
        writer.flush();   
        writer.close();   
        return writer.toString(); 
    }
    
    public static List<LoginInfoBean> parseXML(InputStream in) throws Exception {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(in, "UTF-8");
        int type = parser.getEventType(); 
        
        LoginInfoBean loginInfo = null;
        List<LoginInfoBean> list = null;
        
        while (type != XmlPullParser.END_DOCUMENT) { 
            switch (type) {   
            case XmlPullParser.START_DOCUMENT:   
            	list = new ArrayList<LoginInfoBean>();   
                break;   
   
            case XmlPullParser.START_TAG:
                if ("Table1".equals(parser.getName())) {
                	loginInfo = new LoginInfoBean();
                } else if (null != loginInfo) {
                    if ("dspname".equals(parser.getName())) {   
                    	loginInfo.setDspname(parser.nextText());   
                    } else if("saof".equals(parser.getName())) {
                    	loginInfo.setSaof(parser.nextText());
                    } else if("saofname".equals(parser.getName())) {
                    	loginInfo.setSaofname(parser.nextText());
                    } else if("runstate".equals(parser.getName())) {
                    	loginInfo.setRunstate(parser.nextText());
                    } else if("nowdate".equals(parser.getName())) {
                    	loginInfo.setNowdate(parser.nextText());
                    }
                }   
                break;   
   
            case XmlPullParser.END_TAG:   
                if ("Table1".equals(parser.getName())) {
                    if (null != loginInfo && null != list) {   
                    	list.add(loginInfo);   
                        loginInfo = null;   
                    }   
                }
                break;
            }
            type = parser.next();
        }   
        in.close();   
        return list;  
    }
}
